package cn.hfjava.kz.controller;


import cn.dev33.satoken.util.SaResult;
import cn.hfjava.kz.entity.po.Permission;
import cn.hfjava.kz.entity.vo.TreeSelectVO;
import cn.hfjava.kz.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/perms")
@Tag(name = "菜单权限管理模块")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

//    @GetMapping("/tree")
//    @Operation(summary = "获取权限树数据")
//    public SaResult tableTree(){
//        List<Permission> permsList = permissionService.findAllPermission();
//        List<Permission> permsTreeList = permissionService.buildPermissionTree(permsList);
//        return SaResult.data(permsTreeList);
//    }


    /**
     * 获取权限树数据
     *
     * @return
     */
    @Operation(summary = "获取权限树数据")
    @GetMapping("/menuTree")
    public SaResult menuTree() {
        List<Permission> permList = permissionService.listWithTree();
        return SaResult.data(permList);
    }


    @Operation(summary = "根据菜单名称查询菜单权限树")
    @GetMapping("/menuTreeByName")
    //@SaCheckPermission("menu.list")
    public SaResult treeList(
            @RequestParam(value = "menuName",required = false,defaultValue = "") String menuName) {
        List<Permission> permsTrees = permissionService.findPermsTreeByName(menuName);
        return SaResult.data(permsTrees);
    }

    @Operation(summary = "新增菜单权限")
    @PostMapping("/add")
    public SaResult addMenu(@RequestBody Permission permission) {
        permissionService.save(permission);
        return SaResult.ok("新增菜单成功");
    }


    @Operation(summary = "新增子菜单权限")
    @PostMapping("/addChild/{pid}")
    public SaResult addChildMenu(@PathVariable(value = "pid") Integer pid,
                                 @RequestBody Permission permission) {
        permission.setPid(pid);
        permissionService.save(permission);
        return SaResult.ok("新增子菜单权限成功");
    }


    @Operation(summary = "修改菜单权限")
    @PutMapping("/update")
    public SaResult updateMenu(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return SaResult.ok("修改菜单成功");
    }

    @Operation(summary = "获取菜单权限信息")
    @GetMapping("/get/{id:\\d+}")
    public SaResult getMenu(@PathVariable(value = "id",required = true) Integer id) {
        Permission permInfo = permissionService.getById(id);
        return SaResult.data(permInfo);
    }

    @Operation(summary = "删除菜单权限信息")
    @DeleteMapping("/delete/{id:\\d+}")
    public SaResult deleteMenu(@PathVariable(value = "id",required = true) Integer id) {
        Boolean retVal = permissionService.deleteMenuById(id);
        if(retVal.booleanValue()) {
            return SaResult.ok("删除菜单成功");
        }
        return SaResult.error("删除菜单失败");
    }


    @Operation(summary = "下拉菜单的树形数据")
    @GetMapping("/treeSelect")
    public SaResult getTreeSelect() {
        List<TreeSelectVO> treeSelectTree = permissionService.findTreeSelectTree();
        return SaResult.data(treeSelectTree);
    }

    /**
     * 根据角色ID获取权限树
     *
     * @param roleId 角色ID
     * @return 权限树结构列表
     */
    @GetMapping("/role")
    public SaResult getPermissionTreeByRoleId(@RequestParam Integer roleId) {
        List<Permission> perList = permissionService.getPermissionTreeByRoleId(roleId);
        return SaResult.data(perList);
    }

}